package com.umpay.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.umpay.dict.XmlData;
import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.exception.BusinessException;
import com.umpay.util.HttpUtil;
import com.umpay.util.StringUtil;

/**
 * 
 * @ClassName: HttpUtils  
 * @Description: 请求其他应用Http请求工具类（xml报文） 
 * @author zhangtao  
 * @date Jan 24, 2018
 */

public class RemoteServiceUtils {
	private static final Logger log = LogManager.getLogger(RemoteServiceUtils.class);
	
	/**
	 * 
	 * @Title: callRemoteServiceByPost  
	 * @Description: TODO(这里用一句话描述这个方法的作用)  
	 * @param @param url
	 * @param @param reqMap
	 * @param @return    参数  
	 * @return Map<String,Object>    返回类型  
	 * @throws
	 */
	public static Map<String, Object> callRemoteServiceByPost(String url, Map<String, Object> reqMap) {
		Map<String, Object> retMap = null;
		try {
			if (url.contains("M_Money")) {
				if (url.endsWith("/")) {
					url = url + StringUtils.trim(reqMap.get(XmlData.FUNCODE));
				} else {
					url = url + "/" + StringUtils.trim(reqMap.get(XmlData.FUNCODE));
				}
			}
			log.info(String.format("httpPostXml2Xml请求数据：%s,url=%s", reqMap, url));
			retMap = HttpUtil.httpPostXml2Xml(url, reqMap, "GBK");
			log.info(String.format("httpPostXml2Xml返回结果：%s", retMap));
		} catch (IllegalArgumentException e) {
			log.error("httpPostXml2Xml请求数据错误：" + e.getMessage(), e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "httpPostXml2Xml请求数据错误");
		} catch (IOException e) {
			log.error("IO错误：" + e.getMessage(), e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "IO请求数据错误");
		} catch (Exception e) {
			log.error("未知错误：" + e.getMessage(), e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "未知错误");
		}
		return retMap;
	}
	/**
	 * @Title: callRemoteServiceByPostForm2Xml  
	 * @Description: 调用远程服务
	 * @param @param url
	 * @param @param reqMap
	 * @param @return    参数  
	 * @return Map<String,Object>    返回类型  
	 * @throws
	 */
	public static Map<String, Object> callRemoteServiceByPostForm2Xml(String url,Map<String,Object> reqMap,String charSet){
		Map<String, Object> retMap = null;
		String defaultCharSet="GBK";
		if(!StringUtil.isEmpty(charSet)){
			defaultCharSet=charSet;
		}
		try {
			log.debug(String.format("httpPostForm2Xml请求数据：%s,url=%s" ,reqMap,url));
			retMap = HttpUtil.httpPostForm2Xml(url, reqMap,defaultCharSet,RemoteServiceUtils.getReadTimeout());
			log.info(String.format("httpPostForm2Xml返回结果：%s" , retMap));
		} catch (IllegalArgumentException e) {
			log.error("httpPostForm2Xml请求数据错误：" + e.getMessage(),e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"httpPostForm2Xml请求数据错误");
		} catch (IOException e) {
			log.error("IO错误：" + e.getMessage(),e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"IO请求数据错误");
		} catch (Exception e) {
			log.error("未知错误：" + e.getMessage(),e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"未知错误");
		}
		return retMap;
	}
	/**
	 * @Title: callRemoteServiceByPostXStream  
	 * @Description: 调用前置接口
	 * @param @param url
	 * @param @param reqMap
	 * @param @return    参数  
	 * @return Map<String,Object>    返回类型  
	 * @throws
	 */
	public static Map<String, Object> callRemoteServiceByPostXStream(String url,Map<String,Object> reqMap){
		Map<String, Object> retMap = null;
		try {
			log.debug(String.format("httpPostXStream2XStream请求数据：%s,url=%s" ,reqMap,url));
			retMap = HttpUtil.httpPostXStream2XStream(url, reqMap,"UTF-8");
			log.info(String.format("httpPostXStream2XStream返回结果：%s" , retMap));
		} catch (IllegalArgumentException e) {
			log.error("httpPostXStream2XStream请求数据错误：" + e.getMessage(),e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"httpPostForm2Xml请求数据错误");
		} catch (IOException e) {
			log.error("IO错误：" + e.getMessage(),e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"IO请求数据错误");
		} catch (Exception e) {
			log.error("未知错误：" + e.getMessage(),e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"未知错误");
		}
		return retMap;
	}
	
	/**
	 * 
	 * @Title: getReadTimeout  
	 * @Description: 读取merBusi请求业务网关的超时时间  
	 * @param @return
	 * @param @throws Exception    参数  
	 * @return int    返回类型  
	 * @throws
	 */
	public  static int getReadTimeout() throws Exception{
		int readTOSec = 110;//设置超时时间默认为110，当读取不到配置和缓存时使用
		PropertyUtil propertyUtil = (PropertyUtil) SpringUtil.getBean("propertyUtil");
		try{
			String readTOSecStr = propertyUtil.getProperty(XmlData.READTOSEC_TIMEOUT);//获取参数配置，超时时间
			if(StringUtils.isNotEmpty(readTOSecStr)){//最终获取的时间不为空转换为整型,否则返回默认值
				readTOSec = Integer.parseInt(readTOSecStr);
			}
			log.info(String.format("【存放和读取merBusi请求业务网关的超时时间】读取成功,readTOSecStr:[%s]",readTOSec));
		}catch(Exception e){
			log.info("【存放和读取merBusi请求业务网关的超时时间】getReadTimeout异常，返回默认时间");
		}
		return readTOSec;
	}
}
